import request from 'umi-request';

export interface OidcConfig {
  authorization_endpoint: string;
  token_endpoint: string;
  end_session_endpoint: string;
}

export class OAuthConfig {
  oidcConfigUri!: string;
  clientId!: string;
  responseType!: string;
  scope!: string;
  redirectUri!: string;
  usePkce!: boolean;
  oidcConfig!: OidcConfig;
}

export default function useOAuthConfig(): Promise<OAuthConfig> {
  return new Promise<OAuthConfig>((resolve, reject) => {
    request
      .get('/mobile/config/oauth.json')
      .then((oAuthConfig: OAuthConfig) => {
        request
          .get(oAuthConfig.oidcConfigUri!)
          .then((oidcConfig: OidcConfig) => {
            oAuthConfig.oidcConfig = oidcConfig;
            resolve(oAuthConfig);
          })
          .catch((err) => {
            reject('获取OIDC配置错误');
            throw err;
          });
      })
      .catch((err) => {
        reject('获取OAUTH配置错误');
        throw err;
      });
  });
}
